package ext.tianma.cascade.bean;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Set;

public class CascadeBean extends LinkedHashMap<String, CascadeBean> {

	private static final long serialVersionUID = -8460201508048713372L;
	
	private String lastKey = "";

	public Set<String> get(String... keys) {
		if(keys == null || keys.length == 0) {
			return keySet();
		} else {
			CascadeBean cascade = get(keys[0]);
			if(cascade == null) {
				return null;
			}
			return cascade.get(Arrays.copyOfRange(keys, 1, keys.length));
		}
	}
	
	public CascadeBean() {
		super();
		put("", null);
	}

	public void put(String ...values) {
		if(values != null && values.length > 0) {
			String key = values[0];
			if(key == null || key.isEmpty()) {
				key = lastKey;
			} else {
				lastKey = key;
			}
			CascadeBean cascade = get(key);
			if(cascade == null) {
				cascade = new CascadeBean();
				put(key, cascade);
			}
			cascade.put(Arrays.copyOfRange(values, 1, values.length));
		}
	}
	
}
